window.addEventListener('load', function () {
    //由于这里没有写按钮，先写代码，鼠标经过显示block,鼠标离开隐藏
    var arrow_l = document.querySelector('.arrow_l');
    var arrow_r = document.querySelector('.arrow_r');
    var focus = document.querySelector('.focus');
    var focuswidth = focus.offsetWidth;
    //当鼠标进入foucs盒子里时出现箭头
    focus.addEventListener('mouseenter', function () {
        arrow_l.style.display = 'block';
        arrow_r.style.display = 'block';
        clearInterval(timer);
        timer = null;
    })
    //离开箭头消失
    focus.addEventListener('mouseleave', function () {
        arrow_l.style.display = 'none';
        arrow_r.style.display = 'none';
        timer = setInterval(function () {
            arrow_r.click();
        }, 2000)
    })
    //动态生成轮播图的小圆圈，利用for循环
    var ul = focus.querySelector('ul');//图片里的ul
    var ol = focus.querySelector('ol');//图片里的小圆圈
    var circle = focus.querySelector('.circle');
    /*console.log(ul.children.length)*/
    //根据轮播图的图片确定要插入的小li
    //遍历ul里li并创建ol.li
    for (var i = 0; i < ul.children.length; i++) {
        var li = document.createElement('li')
        //插入ul
        //设置li的自定义属性
        li.setAttribute('index', i);
        ol.appendChild(li);
        //生成li同时绑定点击事件
        li.addEventListener('click', function () {
            //干掉所有人
            for (var i = 0; i < ol.children.length; i++) {
                ol.children[i].className = '';
            }
            //留下自己
            this.className = 'current';
            //5  点击小圆圈，移动图片，移动ul
            //ul的移动距离为li的索引*图片宽度
            //当点击时获得li的索引
            var index = this.getAttribute('index');
            //点击后把索引给num
            num = index;
            circle = index;
            animate(ul, -index * focuswidth);
        })
    }
    //第一个小圆圈
    ol.children[0].className = 'current';
    //6克隆第一张图片放到子厚
    var first = ul.children[0].cloneNode(true);
    ul.appendChild(first);
    //点击右侧按钮，图片滚动一张
    var num = 0;
    var circle = 0;//控制小圆圈播放
    var flag=true;//节流阀
    arrow_r.addEventListener('click', function () {
        if (flag){
            flag=false;//如果到了最后一张图片`
            if (num == ul.children.length - 1) {
                ul.style.left = 0;
                num = 0;
            }
            num++;
            animate(ul, -num * focuswidth,function () {
                flag=true;//打开节流阀
            });
            //点击右侧按钮，小圆圈跟着
            circle++;
            if (circle == ol.children.length) {
                circle = 0;
            }
            circleChange();
        }

    })
    //左侧按钮
    arrow_l.addEventListener('click', function () {
        //如果到了最后一张图片
        if (flag){
            flag=false;
            if (num == 0) {
                num = ul.children.length - 1;
                ul.style.left = -num * focuswidth + 'px';
            }
            num--;
            animate(ul, -num * focuswidth,function () {
                flag=true;
            });
            //点击右侧按钮，小圆圈跟着
            circle--;
            if (circle < 0) {
                circle = ol.children.length - 1;
            }
            circleChange();
        }

    })

    function circleChange() {
        //先清除其余
        for (var i = 0; i < ol.children.length; i++) {
            ol.children[i].className = '';
        }
        ol.children[circle].className = 'current';
    }

    //自动播放
    var timer = setInterval(function () {
        arrow_r.click();
    }, 2000);
})